package com.ciwong.epaper.modules.me.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.epaper.modules.cordva.BaseHtmlActicity;
import com.ciwong.epaper.modules.cordva.SubmitEvent;
import com.ciwong.epaper.modules.me.bean.RequirementContent;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.util.HttpRequest;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.LogOutUtil;
import com.ciwong.eventbus.EventBus;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.libs.utils.CWSys;
import com.ciwong.libs.utils.volley.BaseRequest;
import com.ciwong.mobilelib.ui.BrowserActivity;
import com.ciwong.mobilelib.widget.CWDialog;
import com.google.gson.Gson;

import java.net.URLEncoder;

/**
 * @description:隐私保护声明页面
 * @author: wyk
 * @time: 2021/8/9 9:41
 */
public class AgreementCancelActivity extends BaseHtmlActicity implements View.OnClickListener {
    private static final String TAG = "StudentGradesActivity";

    private String startURL, title;
    private RelativeLayout agreement_cancel_layout;
    private String packageName = "";
    private CWDialog cwDialog;

    @Override
    protected void preCreate() {
        super.preCreate();

    }

    @Override
    protected void findViews() {
        super.findViews();
        agreement_cancel_layout = findViewById(R.id.agreement_cancel_layout);
        agreement_cancel_layout.setVisibility(View.VISIBLE);
    }

    @Override
    protected void init() {
        super.init();
        cwDialog = new CWDialog(this);
        packageName = getPackageName();
        Intent intent = getIntent();
        startURL = intent.getStringExtra(IntentFlag.INTENT_FLAG_AGREEMENT_CANCEL_URL);
        title = intent.getStringExtra(IntentFlag.INTENT_FLAG_TITLE);
        setTitleText(title);
        setStartURL(startURL);
        loadUrl(startURL);
    }

    public static String toUtf8(String str) {
        String result = null;
        try {
            result = URLEncoder.encode(str, "UTF-8");
            result = result.replace("+", "%20");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 显示弹框是否注销
     */
    private void showDialog() {
            if (null != cwDialog) {
                cwDialog.setTitle(R.string.agreement_cancel_dialog_title);
                cwDialog.setMessage(R.string.agreement_calcel_dialog_message);
                cwDialog.setNegativeButton(getString(R.string.agreement_cancel_dialog_cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        CWSys.setSharedBoolean("isFirstUse" + packageName, true);//设置是第一次登录
                        try {
                            CWSys.setSharedBoolean("privacyAgreement" + packageName + getPackageManager()
                                    .getPackageInfo(getPackageName(), 0).versionName, false);//设置不同意隐私协议
                        } catch (PackageManager.NameNotFoundException e) {
                            e.printStackTrace();
                        }
                        CWSys.setSharedBoolean("agreement" + packageName, false);//设置不同意政策
                        LogOutUtil.logOut(AgreementCancelActivity.this, true);
                        cwDialog.dismiss();
                    }
                });
                SpannableStringBuilder stringBuilder = new SpannableStringBuilder(getString(R.string.agreement_cancel_dialog_agree));
                ForegroundColorSpan RedSpan = new ForegroundColorSpan(Color.parseColor("#5ce191"));
                stringBuilder.setSpan(RedSpan, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                cwDialog.setPositiveButton(stringBuilder, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        cwDialog.dismiss();
                    }
                });
                cwDialog.show();
            }
    }


    @Override
    protected void loadData() {
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        agreement_cancel_layout.setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }


    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void finish() {
        if (webView.canGoBack()) {
            webView.goBack();
            return;
        }
        super.finish();
    }


    public void onEventMainThread(SubmitEvent submitEvent) {
        CWLog.d(TAG, "onEventMainThread+" + submitEvent.getId() + "--Json:" + submitEvent.getJson());
        if (submitEvent.getId().equals("JumpAgreementCancel")) {//
            /*JumpServiceParamBean jumpServiceParaBean = gson.fromJson(submitEvent.getJson(), JumpServiceParamBean.class);
            int serviceId = jumpServiceParaBean.getServiceId();
            String urlH5 = jumpServiceParaBean.getUrl();*/
            jumpToInnerResouce(submitEvent.getJson());
        }
    }

    /**
     * 跳入h5内部资源
     *
     * @param urlH5
     */
    public void jumpToInnerResouce(String urlH5) {
        if (!urlH5.contains("http")) {//解决连接中不包含http 导致闪退的bug
            urlH5 = "http://" + urlH5;
        }
        MeJumpManager.jumpToResourseCenterInnerH5Activity(R.string.go_back, this, urlH5);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.agreement_cancel_layout) {
            showDialog();
        }
    }
}
